<template>
  <a-spin :spinning="confirmLoading">
    <JFormContainer :disabled="disabled">
      <template #detail>
        <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol" name="CustomerForm">
          <a-row>
						<a-col :span="24">
							<a-form-item label="头像" v-bind="validateInfos.profilePicture" id="CustomerForm-profilePicture" name="profilePicture">
								<j-image-upload :fileMax="0" v-model:value="formData.profilePicture" ></j-image-upload>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="客户名称" v-bind="validateInfos.customerName" id="CustomerForm-customerName" name="customerName">
								<a-input v-model:value="formData.customerName" placeholder="请输入客户名称"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="公司名称" v-bind="validateInfos.companyName" id="CustomerForm-companyName" name="companyName">
								<a-input v-model:value="formData.companyName" placeholder="请输入公司名称"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="会员等级" v-bind="validateInfos.membershipLevel" id="CustomerForm-membershipLevel" name="membershipLevel">
								<j-dict-select-tag v-model:value="formData.membershipLevel" dictCode="membership_level" placeholder="请选择会员等级"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="客户级别" v-bind="validateInfos.customerLevel" id="CustomerForm-customerLevel" name="customerLevel">
								<j-dict-select-tag v-model:value="formData.customerLevel" dictCode="customer_level" placeholder="请选择客户级别"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="客户标签" v-bind="validateInfos.customerLabel" id="CustomerForm-customerLabel" name="customerLabel">
								<j-dict-select-tag v-model:value="formData.customerLabel" dictCode="customer_label" placeholder="请选择客户标签"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="账户余额" v-bind="validateInfos.accountBalance" id="CustomerForm-accountBalance" name="accountBalance">
								<a-input-number v-model:value="formData.accountBalance" placeholder="请输入账户余额" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="累计消费" v-bind="validateInfos.accumulatedConsumption" id="CustomerForm-accumulatedConsumption" name="accumulatedConsumption">
								<a-input-number v-model:value="formData.accumulatedConsumption" placeholder="请输入累计消费" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="业务人员" v-bind="validateInfos.businessPersonnel" id="CustomerForm-businessPersonnel" name="businessPersonnel">
								<j-search-select v-model:value="formData.businessPersonnel" dict=""  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="客服人员" v-bind="validateInfos.customerServicePersonnel" id="CustomerForm-customerServicePersonnel" name="customerServicePersonnel">
								<j-search-select v-model:value="formData.customerServicePersonnel" dict=""  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="账户类型" v-bind="validateInfos.accountType" id="CustomerForm-accountType" name="accountType">
								<j-dict-select-tag v-model:value="formData.accountType" dictCode="account_type" placeholder="请选择账户类型"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="项目数" v-bind="validateInfos.item" id="CustomerForm-item" name="item">
								<a-input-number v-model:value="formData.item" placeholder="请输入项目数" style="width: 100%" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="电子邮箱" v-bind="validateInfos.email" id="CustomerForm-email" name="email">
								<a-input v-model:value="formData.email" placeholder="请输入电子邮箱"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="最后交易" v-bind="validateInfos.finalTransaction" id="CustomerForm-finalTransaction" name="finalTransaction">
								<a-date-picker placeholder="请选择最后交易"  v-model:value="formData.finalTransaction" showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="微信名称" v-bind="validateInfos.wechatName" id="CustomerForm-wechatName" name="wechatName">
								<a-input v-model:value="formData.wechatName" placeholder="请输入微信名称"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="电话号码" v-bind="validateInfos.phoneNumber" id="CustomerForm-phoneNumber" name="phoneNumber">
								<a-input v-model:value="formData.phoneNumber" placeholder="请输入电话号码"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="微信账号" v-bind="validateInfos.wechatAccount" id="CustomerForm-wechatAccount" name="wechatAccount">
								<a-input v-model:value="formData.wechatAccount" placeholder="请输入微信账号"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="公司地址" v-bind="validateInfos.companyAddress" id="CustomerForm-companyAddress" name="companyAddress">
								<a-textarea v-model:value="formData.companyAddress" :rows="4" placeholder="请输入公司地址" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="备注" v-bind="validateInfos.remark" id="CustomerForm-remark" name="remark">
								<a-textarea v-model:value="formData.remark" :rows="4" placeholder="请输入备注" />
							</a-form-item>
						</a-col>
          </a-row>
        </a-form>
      </template>
    </JFormContainer>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
  import JSearchSelect from '/@/components/Form/src/jeecg/components/JSearchSelect.vue';
  import JImageUpload from '/@/components/Form/src/jeecg/components/JImageUpload.vue';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../Customer.api';
  import { Form } from 'ant-design-vue';
  import JFormContainer from '/@/components/Form/src/container/JFormContainer.vue';
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: () => ({})},
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    profilePicture: '',   
    customerName: '',   
    companyName: '',   
    membershipLevel: undefined,
    customerLevel: undefined,
    customerLabel: undefined,
    accountBalance: undefined,
    accumulatedConsumption: undefined,
    businessPersonnel: '',   
    customerServicePersonnel: '',   
    accountType: undefined,
    item: undefined,
    email: '',   
    finalTransaction: '',   
    wechatName: '',   
    phoneNumber: '',   
    wechatAccount: '',   
    companyAddress: '',   
    remark: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = reactive({
    customerName: [{ required: true, message: '请输入客户名称!'},],
  });
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    try {
      // 触发表单验证
      await validate();
    } catch ({ errorFields }) {
      if (errorFields) {
        const firstField = errorFields[0];
        if (firstField) {
          formRef.value.scrollToField(firstField.name, { behavior: 'smooth', block: 'center' });
        }
      }
      return Promise.reject(errorFields);
    }
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    padding: 14px;
  }
</style>
